package com.lantosec.hmrp.modules.internal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.internal.entity.InternalItem;
import com.lantosec.hmrp.modules.internal.entity.InternalType;
import com.lantosec.hmrp.modules.internal.mapper.InternalItemMapper;
import com.lantosec.hmrp.modules.internal.service.IInternalItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.internal.vo.InternalItemVO;
import com.lantosec.hmrp.modules.internal.vo.InternalTypeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 项目定义 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-22
 */
@Service
@Primary
@Log4j2
public class InternalItemServiceImpl extends ServiceImpl<InternalItemMapper, InternalItem> implements IInternalItemService {
    @Override
    public String queryMaxId() {
        String internalItemId = baseMapper.queryMaxId();
            return StringUtils.isEmpty(internalItemId) ? "001" : internalItemId;
        }

    @Override
    public List<InternalItemVO> getAll(String internalItemName, Integer internalItemType, Integer internalTypeId) {
        List<InternalItemVO> internalItemVOList = baseMapper.getAll(internalItemName,internalItemType,internalTypeId);
        return internalItemVOList;
    }

    @Override
    public List<InternalTypeVO> getInternalItem(Integer internalItemType) {
        List<InternalTypeVO> internalTypeVOList = new ArrayList<>();
        //查询项目类型
        List<InternalType> internalTypes = baseMapper.getItemType(internalItemType);
        internalTypes.forEach(param->{
            InternalTypeVO internalTypeVO = param.convertToVO();
            //查询项目类型下的项目
            List<InternalItem> internalItems = this.list(new QueryWrapper<InternalItem>().eq(InternalItem.ColName.internal_item_type.toString(),internalItemType)
                                                                                            .eq(InternalItem.ColName.internal_type_id.toString(),param.getInternalTypeId()));
            internalTypeVO.setInternalItems(internalItems);
            internalTypeVOList.add(internalTypeVO);
        });
        return internalTypeVOList;
    }
}
